package com.chenfan.process.dto;

import com.chenfan.process.enums.CarbonCopyEnum;
import com.chenfan.process.enums.EventTypeEnums;
import com.chenfan.process.enums.LinkedTypeEnums;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author K
 * @date 2021/1/27
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ApprovalCallBack implements Serializable {
    @ApiModelProperty(value = "审批ID", example = "1353956254594105344", reference = "从审批界面创建后返回的审批id")
    private Long approvalId;

    @ApiModelProperty(value = "服务名称", example = "chenfan-cloud-process")
    private String serviceName;

    @ApiModelProperty(value = "回调参数", example = "{\"id\": \"1\"}", reference = "审批流发起的时候传递过来的参数")
    private String param;

    @Deprecated
    @ApiModelProperty(value = "事件类型", example = "0")
    private Integer eventType;

    @ApiModelProperty(value = "事件类型值", example = "创建", reference = "com.chenfan.process.enums.EventTypeEnums")
    private String eventTypeValue;

    public String getEventTypeValue() {
        return EventTypeEnums.getMessage(eventType);
    }

    @ApiModelProperty(value = "审批结果", example = "true")
    private Boolean approvalFlag;

    @ApiModelProperty(value = "审批是否结束", example = "true")
    private Boolean approvalFinished;

    @ApiModelProperty(value = "审批信息，可能有多个用户", example = "1,2")
    private String nextApprovalUserId;

    @ApiModelProperty(value = "审批用户名称, 可能有多个用户", example = "大明,小明")
    private String nextApprovalUserName;

    @ApiModelProperty(value = "下一个审批节点名称, 可能有多个", example = "HR,财务")
    private String nextNodeName;

    @ApiModelProperty(value = "评论备注信息", example = "审批拒绝理由")
    private String remark;

    @ApiModelProperty(value = "是否属于发起", example = "true")
    private boolean start;

    @ApiModelProperty(value = "抄送人", reference = "com.chenfan.process.dto.ApprovalCallBack.CarbonCopyDTO")
    private List<CarbonCopyDTO> carbonCopyList;

    @ApiModelProperty(value = "用户ID", example = "1")
    private Long userId;

    @ApiModelProperty(value = "用户名称", example = "大明")
    private String userName;

    @ApiModelProperty(value = "节点名称", example = "财务经理")
    private String nodeName;

    @ApiModelProperty(value = "是否相同节点", example = "true")
    private Boolean skipped;

    @ApiModelProperty(value = "审批类型", example = "直接使用isOrSign()方法确认是否会签")
    private Integer linkType;

    @JsonIgnore
    public boolean isOrSign() {
        return LinkedTypeEnums.SERIAL.getCode().equals(linkType);
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CarbonCopyDTO {
        @ApiModelProperty(value = "用户ID", example = "1")
        private Long userId;
        @ApiModelProperty(value = "用户名称", example = "大明")
        private String userName;
        @ApiModelProperty(value = "审批类型", example = "1")
        private Integer type;
        @ApiModelProperty(value = "跳转路由", example = "http://www.baidu.com")
        private String url;
        @ApiModelProperty(value = "节点名称", example = "财务经理")
        private String nodeName;

        @JsonIgnore
        public boolean isSpecialNode() {
            return Objects.equals(userId, -1L);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (!(o instanceof CarbonCopyDTO))
                return false;
            CarbonCopyDTO that = (CarbonCopyDTO)o;
            return Objects.equals(getUserId(), that.getUserId()) && Objects.equals(getUserName(), that.getUserName());
        }

        @Override
        public int hashCode() {
            return Objects.hash(getUserId(), getUserName());
        }

        public boolean filter() {
            return (!Objects.equals(getType(), CarbonCopyEnum.ALL.getCode())
                && !Objects.equals(getType(), CarbonCopyEnum.APPROVAL_CREATE_PERSON.getCode())
            && !isSpecialNode());
        }
    }

}
